package com.mspbots.core.wise.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mspbots.core.AbstractBaseController;
import com.mspbots.core.wise.controller.params.TenantUserParams;
import com.mspbots.core.wise.model.TenantUser;
import com.mspbots.core.wise.service.TenantUserService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.concurrent.Callable;


/**
 * @author billb
 */
@RestController
@RequestMapping("{tenant}/user")
public class TenantUserController extends AbstractBaseController<TenantUser, TenantUserService> {

    @PostMapping("add-batch")
    public Callable batchAll(@RequestBody List<TenantUser> tenantUsers) {
        return () -> {
            this.service.saveBatch(tenantUsers);
            return tenantUsers;
        };
    }

    @Override
    @GetMapping("page")
    public Callable selectPage(Page<TenantUser> page, TenantUser queryParams) {
        return () -> this.service.getPage(page, TenantUserParams.builder().tenantId(queryParams.getTenantId())
                .username(queryParams.getFirstname() + " " + queryParams.getLastname()).build());
    }

    @Override
    @GetMapping(path = "/list")
    public Callable selectList(TenantUser queryParams) {
        QueryWrapper<TenantUser> queryWrapper = new QueryWrapper<>(queryParams);
        queryWrapper.ne(!StringUtils.isEmpty(queryParams.getLicenseClassNot()), "license_class", queryParams.getLicenseClassNot());
        return () -> service.list(queryWrapper);
    }

    @GetMapping("search")
    public Callable selectPage(Page<TenantUser> page, TenantUserParams queryParams) {
        return () -> this.service.getPage(page, queryParams);
    }

    @GetMapping("identifier")
    public Callable selectIdentifier(Long tenantId) {
        return () -> {
            List<TenantUser> tenantUsers = this.service.list(Wrappers.<TenantUser>lambdaQuery()
                    .eq(TenantUser::getEnable, true)
                    .eq(TenantUser::getTenantId, tenantId));
            List<Map<String, String>> userList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(tenantUsers)) {
                tenantUsers.stream().sorted(Comparator.comparing(TenantUser::getFirstname)).forEach(tk ->
                        userList.add(new HashMap<>() {{
                            put("identifier", tk.getIdentifier());
                            put("userName", tk.getFirstname() + " " + tk.getLastname());
                        }}));
            }
            return userList;
        };
    }

    @GetMapping("search/email")
    public Callable selectEmail(TenantUser t) {
        return () -> this.service.list((Wrappers.<TenantUser>lambdaQuery().like(TenantUser::getEmail, t.getEmail()).eq(TenantUser::getTenantId, t.getTenantId())));
    }

    @GetMapping("last-one")
    public Callable lastOne(@PathVariable Long tenant) {
        return () -> this.service.lastOne(tenant);
    }

    @GetMapping("count")
    public Callable count(@RequestParam Long tenantId) {
        return () -> this.service.count(Wrappers.<TenantUser>lambdaQuery().eq(TenantUser::getTenantId, tenantId));
    }

    @GetMapping(path = "info")
    public Callable selectOne(TenantUser t) {
        return () -> service.getOne(new QueryWrapper<>(t));
    }

    @GetMapping(path = "selectByIdentifier")
    public Callable selectOneByIdentifier(TenantUser t) {
        return () -> this.service.selectOneByIdentifier(t);
    }

    @GetMapping("selectByManagerId/{manager}")
    public Callable selectByManagerId(@PathVariable Long manager) {
        return () -> service.selectByManagerId(manager);
    }

    @PutMapping("updateAll/{id}")
    public Callable update(@PathVariable Long id, @RequestBody TenantUser entity) {
        return () -> service.updateAll(id, entity);
    }
}
